/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.ruby_string.delayed;

import moved.org.objectweb.asm.Label;
import moved.org.objectweb.asm.MethodVisitor;

public class LocalVariableInstruction extends AbstractMethodInstruction {
	private final String name;
	private final String desc;
	private final String signature;
	private final Label start;
	private final Label end;
	private final int index;

	public LocalVariableInstruction(String name, String desc, String signature, Label start,
		Label end, int index) {
		
		this.name = name;
		this.desc = desc;
		this.signature = signature;
		this.start = start;
		this.end = end;
		this.index = index;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// MethodInstruction Methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	public void accept(MethodVisitor methodVisitor) {
		methodVisitor.visitLocalVariable(name, desc, signature, start, end, index);
	}
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Object Methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public boolean equals(Object object) {
		if (object == null || !(object.getClass().equals(getClass()))) {
			return false;
		}
		
		LocalVariableInstruction other = (LocalVariableInstruction) object;
		
		return name.equals(other.name) &&
			desc.equals(other.desc) &&
			signature.equals(other.signature) &&
			start.equals(other.start) &&
			end.equals(other.end) &&
			index == other.index;
	}

	public void update(LdcInstruction ldcInstruction) {
		ldcInstruction.update(name, desc, signature, start, end, index);
	}

	public void update(NamedVarInstruction namedVarInstruction, String variableName) {
		if (name.equals(variableName)) {
			namedVarInstruction.setVariableIndex(desc, index);
		}	
	}
}
